package com.kanshu.ksgb.fastread.doudou.common.hotfix.tinker;

import com.kanshu.ksgb.fastread.doudou.common.util.FileUtils;
import com.tencent.tinker.lib.library.TinkerLoadLibrary;
import com.tencent.tinker.lib.tinker.Tinker;
import com.tencent.tinker.lib.tinker.TinkerInstaller;
import com.tencent.tinker.loader.shareutil.ShareConstants;
import com.tencent.tinker.loader.shareutil.ShareTinkerInternals;

import java.io.File;

public class TinkerHelper {

    public static void loadPatch() {
        TinkerInstaller.onReceiveUpgradePatch(TinkerApp.application, FileUtils.getPatchFilePath());
    }

    public static void loadLib() {
        TinkerLoadLibrary.installNavitveLibraryABI(TinkerApp.application, "armeabi");
        System.loadLibrary("stlport_shared");
    }

    public static void removePatch() {
        Tinker.with(TinkerApp.application).cleanPatch();
        File file = new File(FileUtils.getPatchFilePath());
        if(file.exists()){
            file.delete();
        }
    }

    public static void killSelf() {
        ShareTinkerInternals.killAllOtherProcess(TinkerApp.application);
        android.os.Process.killProcess(android.os.Process.myPid());
    }

    public static String showTinkerInfo() {
        final StringBuilder sb = new StringBuilder();
        Tinker tinker = Tinker.with(TinkerApp.application);
        if (tinker.isTinkerLoaded()) {
            sb.append(String.format("[patch is loaded] \n"));
            sb.append(String.format("[buildConfig TINKER_ID] %s \n", BuildInfo.TINKER_ID));
            sb.append(String.format("[buildConfig BASE_TINKER_ID] %s \n", BaseBuildInfo.BASE_TINKER_ID));

            sb.append(String.format("[buildConfig MESSSAGE] %s \n", BuildInfo.MESSAGE));
            sb.append(String.format("[TINKER_ID] %s \n", tinker.getTinkerLoadResultIfPresent().getPackageConfigByName(ShareConstants.TINKER_ID)));
            sb.append(String.format("[packageConfig patchMessage] %s \n", tinker.getTinkerLoadResultIfPresent().getPackageConfigByName("patchMessage")));
            sb.append(String.format("[TINKER_ID Rom Space] %d k \n", tinker.getTinkerRomSpace()));

        } else {
            sb.append(String.format("[patch is not loaded] \n"));
            sb.append(String.format("[buildConfig TINKER_ID] %s \n", BuildInfo.TINKER_ID));
            sb.append(String.format("[buildConfig BASE_TINKER_ID] %s \n", BaseBuildInfo.BASE_TINKER_ID));

            sb.append(String.format("[buildConfig MESSSAGE] %s \n", BuildInfo.MESSAGE));
            sb.append(String.format("[TINKER_ID] %s \n", ShareTinkerInternals.getManifestTinkerID(TinkerApp.application)));
        }
        sb.append(String.format("[BaseBuildInfo Message] %s \n", BaseBuildInfo.TEST_MESSAGE));
        return sb.toString();
    }
}
